package com.ryx.controller;

import com.ryx.constants.Constant;
import com.ryx.entity.SysFile;
import com.ryx.service.FileService;
import com.ryx.utils.DataResult;
import com.ryx.utils.JWTTokenUtil;
import com.ryx.vo.req.FilePageReqVo;
import com.ryx.vo.resp.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @BelongsPackage: com.ryx.controller
 * @Author: 容永轩
 * @CreateTime: 2021-01-28
 * @Description:
 */
@RestController
@RequestMapping("/api")
@Api(tags = "文件管理",description = "文件操作相关接口")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/file")
    @ApiOperation(value = "文件上传接口")
    public DataResult uploadFile(@RequestParam(value = "file")MultipartFile file, HttpServletRequest request){
        String createId = JWTTokenUtil.getUserId( request.getHeader(Constant.ACCESS_TOKEN));

        //文件类型
        Integer type = request.getIntHeader(Constant.FILE_TYPE);

        String fileUrl = fileService.upload(file,createId, type);

        DataResult result = DataResult.success();

        result.setData(fileUrl);

        return result;
    }

    @GetMapping("/file/{fileId}")
    @ApiOperation(value = "文件下载接口")
    public DataResult downloadFile(@PathVariable("fileId") String fileId, HttpServletResponse response){
        fileService.downloadFile(fileId,response);
        DataResult result =DataResult.success();

        return  result;
    }

    @PostMapping("/files")
    @ApiOperation(value = "分页获取我的文件接口")
    public DataResult<PageVo<SysFile>> pageInfo(@RequestBody FilePageReqVo vo,HttpServletRequest request){

        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JWTTokenUtil.getUserId(accessToken);

        DataResult result = DataResult.success();
        result.setData(fileService.pageInfo(vo,userId));

        return  result;
    }





}
